package com.fudan.adse.controller;


import com.fudan.adse.api.CommonPage;
import com.fudan.adse.api.CommonResult;
import com.fudan.adse.domain.po.Cinema;
import com.fudan.adse.domain.po.Room;
import com.fudan.adse.domain.vo.CinemaWithDetailVO;
import com.fudan.adse.service.CinemaService;
import com.fudan.adse.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author zhl
 */
@RestController
@RequestMapping("/api/v1/cms")
public class CinemaController {
    private CinemaService cinemaService;
    private RoomService roomService;

    @PostMapping(value = "/room")
    public CommonResult<Room> addRoom(@RequestBody Room room) {
        return CommonResult.success(roomService.addRoom(room));
    }

    @PostMapping(value = "/cinema")
    public CommonResult<Cinema> addCinema(@RequestBody Cinema cinema) {
        return CommonResult.success(cinemaService.addCinema(cinema));
    }

    @PutMapping(value = "/cinema")
    public CommonResult updateCinemaDetail(@RequestBody Cinema cinema) {
        cinemaService.updateCinema(cinema);
        return CommonResult.success(null);
    }

    @DeleteMapping(value = "/cinema")
    public CommonResult deleteMuseum(@NotBlank @RequestParam("cinemaId") Integer cinemaId) {
        cinemaService.deleteCinema(cinemaId);
        return CommonResult.success(null);
    }

    /**
     * 按条件筛选影院信息，不排除禁用
     *
     * @param locationId
     * @param locationDetail
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/cinemas")
    public CommonResult<CommonPage<CinemaWithDetailVO>> getCinemasByLocation(
            @RequestParam(value = "locationId", required = false) Integer locationId,
            @RequestParam(value = "locationDetail", required = false, defaultValue = "") String locationDetail,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(cinemaService.loadCinemaByLocation(locationId, locationDetail,
                null, pageNum, pageSize)));
    }

    @GetMapping(value = "/rooms")
    public CommonResult<List<Room>> getRoomsById(
            @RequestParam(value = "cinemaId") Integer cinemaId) {
        return CommonResult.success(cinemaService.listRoomsById(cinemaId));
    }

    @Autowired
    public void setCinemaService(CinemaService cinemaService) {
        this.cinemaService = cinemaService;
    }

    @Autowired
    public void setRoomService(RoomService roomService) {
        this.roomService = roomService;
    }
}
